﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Benefit.Models.Sys;
using Benefit.Models.Year;
namespace Benefit.Service.Year
{
    public class ServiceBatchAvgYear : Benefit.Interface.Year.IBatchAvgYear
    {
        Benefit.DB.DBManager db = null;
        public ServiceBatchAvgYear(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 删除当周所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int year)
        {
            var query = from t in db.BatchAvgYear where t.Year == year select t;
            foreach (Models.Year.BatchAvgYear c in query)
            {
                db.BatchAvgYear.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存月同批次交易员均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBatchAvgYear(int year)
        {
            List<Models.Sys.Batch> batch = db.Batches.ToList();
            foreach (Models.Sys.Batch b in batch)
            {
                BatchAvgYear w = new BatchAvgYear();
                w.BatchId = b.Id;
                w.Year = year;

                var queryExist = from t in db.BatchAvgYear where t.BatchId == b.Id && t.Year == year select t;
                if (queryExist.Count() > 0)
                {
                    w = queryExist.First();
                }


                var query = from t in db.BatchAvgDay where t.TradeHistory.YPdate == year && t.BatchId == b.Id select t;
                if (query.Count() > 0)
                {
                    w.BillCount = query.Average(a => a.BillCount);
                    w.DayCount = query.Average(a => a.DayCount);
                    w.Free = query.Average(a => a.Free);
                    w.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                    w.Profit = query.Average(a => a.Profit);
                    w.SumLost = query.Average(a => a.SumLost);
                    w.SumWin = query.Average(a => a.SumWin);
                    w.UsedMargin = query.Average(a => a.UsedMargin);
                    w.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                    w.AvgBillBenefit = query.Average(a => a.AvgBillBenefit);
                    w.AvgPostionTime = query.Average(a => a.AvgPostionTime);
                    w.MaxReturn = query.Average(a => a.MaxReturn);
                    w.OverStopLoss = query.Average(a => a.OverStopLoss);
                    w.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                    w.RaiseRate = query.Average(a => a.RaiseRate);
                    w.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                    w.Utilization = query.Average(a => a.Utilization);
                    w.WinRate = query.Average(a => a.WinRate);
                    w.Yield = query.Average(a => a.Yield);
                    w.Year = query.First().TradeHistory.YPdate;
                    if (queryExist.Count() == 0)
                    {
                        db.BatchAvgYear.Add(w);
                    }
                }
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取同批次交易员年均值
        /// </summary>
        /// <param name="batchid"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public BatchAvgYear GetBatchAvgYear(int batchid, int year)
        {
            var query = db.BatchAvgYear.Where(a => a.BatchId == batchid).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new Models.Year.BatchAvgYear();
            }
        }


    }
}
